package nl.saxion.ein4b;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import nl.saxion.ein4b.model.Card;
import nl.saxion.ein4b.model.CardColor;
import nl.saxion.ein4b.model.CardType;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PorterDuff;
import android.graphics.RectF;
import android.view.View;

/**
 * 
 * @author Harm
 * 
 */

class GameView extends View {
	private Paint paint;
	private Canvas gameCanvas;
	private Bitmap gameBitmap;

	private HashMap<String, Bitmap> cards = new HashMap<String, Bitmap>();
	private HashMap<Integer, Bitmap> coins = new HashMap<Integer, Bitmap>();

	public GameView(Context context) {
		super(context);
		paint = new Paint();
		paint.setColor(Color.WHITE);
		paint.setStrokeWidth(2.0f);
		gameBitmap = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
		gameCanvas = new Canvas(gameBitmap);

		paint.setTextSize(30);

		loadResources();

	}

	public void drawCard(Card card, int x, int y) {
		if (card == null)
			gameCanvas.drawBitmap(cards.get("back"), x, y, null);
		else
			gameCanvas.drawBitmap(cards.get(card.toString()), x, y, null);
	}

	private void loadResources() {
		// back of card
		InputStream in = null;
		try {
			in = this.getResources().getAssets().open("cards/b2fv.png");
		} catch (IOException e) {
			;
		}

		Bitmap bm1 = BitmapFactory.decodeStream(in);
		cards.put("back", bm1);

		// load all the cards
		for (CardColor cc : CardColor.values()) {
			for (CardType ct : CardType.values()) {
				String path = "cards/";
				if (cc == CardColor.CLUBS)
					path += "c";
				if (cc == CardColor.DIAMONDS)
					path += "d";
				if (cc == CardColor.HEARTS)
					path += "h";
				if (cc == CardColor.SPADES)
					path += "s";
				if (ct == CardType.JACK)
					path += "j";
				else if (ct == CardType.QUEEN)
					path += "q";
				else if (ct == CardType.KING)
					path += "k";
				else
					path += ct.getValue();
				path += ".png";

				InputStream is = null;
				try {
					is = this.getResources().getAssets().open(path);
				} catch (IOException e) {
					;
				}

				Bitmap bm = BitmapFactory.decodeStream(is);
				if (bm == null)
					System.out.println(path);
				cards.put(new Card(ct, cc).toString(), bm);
			}
		}

		int[] cvalues = { 5, 10, 25, 50, 100 };

		for (int c : cvalues) {
			String path = "coins/" + c + "coin.png";
			InputStream is = null;
			try {
				is = this.getResources().getAssets().open(path);
			} catch (IOException e) {
				;
			}
			Bitmap bm = BitmapFactory.decodeStream(is);
			coins.put(c, bm);
		}
	}

	@Override
	public void onDraw(Canvas canvas) {
		canvas.drawBitmap(gameBitmap, 0, 0, null);
		invalidate();
	}

	public void clear() {
		gameCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
	}

	public void drawCoin(int value, int x, int y) {
		gameCanvas.drawBitmap(coins.get(value), x, y, null);
	}

	public void drawText(String text, int x, int y) {
		gameCanvas.drawText(text, x, y, paint);
	}

	public void drawOval(int x, int y, int h, int w) {
		paint.setStyle(Style.STROKE);
		RectF oval = new RectF(x, y, x + w, y + h);
		gameCanvas.drawOval(oval, paint);
		paint.setStyle(Style.FILL);
	}

}